package dream.web;

import com.alibaba.fastjson.JSON;
import dream.pojo.Article;
import dream.service.ArticleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/artlist")
public class ShowBlogByPageServlet extends HttpServlet {
    private ArticleService articleService = new ArticleService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currPage = req.getParameter("cp");
        String pageSize = req.getParameter("ps");

        List<Article> articles =  articleService.showBlogByPageServlet((Integer.valueOf(currPage) -1)*Integer.valueOf(pageSize), Integer.valueOf(pageSize));

        // 转为JSON
        String jsonString = JSON.toJSONString(articles);

        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);

    }
}
